package com.woniu108.carService.commos.util;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Component
public class RedisUtil {
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Resource
    private RedisTemplate<String,Object> redisObjectTemplate;

    /**
     * 读取数据
     */
    public String getString(String key) {
        return stringRedisTemplate.opsForValue().get(key);
    }

    /**
     * 写入数据
     */
    public void setString(String key, String value) {
        stringRedisTemplate.opsForValue().set(key, value);
    }
    /**
     * 写入数据并设置有效时间
     */
    public  void setString(String key, String value, long seconds) {
        stringRedisTemplate.opsForValue().set(key, value,seconds, TimeUnit.SECONDS);
    }
    /**
     * 删除数据
     */
    public void delete(String key) {
        stringRedisTemplate.delete(key);
    }


    public boolean exist(String key) {
        return stringRedisTemplate.hasKey(key);
    }

    public void incr(String key){
        stringRedisTemplate.opsForValue().increment(key);
    }



    public Double zscore(String key,String value){
        //获取一个数据在ZSET的排序值
        return stringRedisTemplate.opsForZSet().score(key,value);
    }

    public void zadd(String key,String value,double score){
        //向ZSET新增一个数据
        stringRedisTemplate.opsForZSet().add(key,value,score);
    }
    public void zincreby(String key,String value,double number){
        //使ZSET中的某个数据排序值自增
        stringRedisTemplate.opsForZSet().incrementScore(key,value,number);
    }

    public Set<String> zreverange(String key,Integer start,Integer end){
        return stringRedisTemplate.opsForZSet().reverseRange(key,start,end);
    }

    public long zcard(String key){
        return stringRedisTemplate.opsForZSet().zCard(key);
    }

    //删除一个zset数据
    public void zsrem(String key,String value) throws Exception{
        stringRedisTemplate.opsForZSet().remove(key, value);
    }

    //新增一组hash数据
    public void hmset(String key, HashMap<String,Object> map){
        stringRedisTemplate.opsForHash().putAll(key,map);
    }

    public void hmset1(String key,String hashKey,String number){
        //修改数据
        stringRedisTemplate.opsForHash().put(key,hashKey,number);
    }

    //使hash中的一个数据自增
    public void hincrby(String key,String hashKey,Integer number){
        stringRedisTemplate.opsForHash().increment(key,hashKey,number);
    }

    public String hget(String key,String hashkey){
        return (String) stringRedisTemplate.opsForHash().get(key,hashkey);
    }

    public Map<Object, Object> hgetall(String key){
        return stringRedisTemplate.opsForHash().entries(key);
    }

    //新增一个对象数据，以字符串形式保存到redis,有过期时间(序列化)
    public void setObject(String key,Object value,long minute){
        redisObjectTemplate.opsForValue().set(key,value,minute,TimeUnit.MINUTES);
    }

    //新增一个对象数据，以字符串形式保存到redis,无过期时间(序列化)
    public void setObject(String key,Object value){
        redisObjectTemplate.opsForValue().set(key,value);
    }

    //获取一个对象数据(反序列化)
    public Object getObject(String key){
        return redisObjectTemplate.opsForValue().get(key);
    }

    /**
     * 删除ZSet一个数据
     * @param Key
     * @param value
     */
    public void zrem(String Key,String value){
        stringRedisTemplate.opsForZSet().remove(Key,value);
    }

    /**
     * 向set中添加数据
     * @param Key
     * @param value
     */
    public void sAdd(String Key, String value) {
        redisObjectTemplate.opsForSet().add(Key,value);
    }

    /**
     * 删除Set中的一个元素
     * @param Key
     * @param value
     */
    public void sRem(String Key,String value){
        redisObjectTemplate.opsForSet().remove(Key,value);
    }

    public Set<Object> sMembers(String Key){
        return redisObjectTemplate.opsForSet().members(Key);
    }
}
